package com.activity.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.activity.PersonInfoActivity;
import com.activity.bean.ChooseTimeBean;
import com.activity.bean.ClassSpan;
import com.activity.bean.EmployerBean;
import com.activity.shop.AddressChooseActivity;
import com.activity.shop.AttanceMemberActivity;
import com.activity.shop.ClassActivity;
import com.activity.shop.TimeChooseActivity;
import com.amap.api.services.core.PoiItem;
import com.api.param.UpdateUserParam;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.widget.Dialog;
import com.example.apple.ddhr_new.adapter.BaseAdapter;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.google.gson.Gson;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import butterknife.BindView;

public class CreateSheetGroupAdapter extends BaseAdapter<PoiItem> {


    public ArrayList<EmployerBean> members = new ArrayList();
    public ClassSpan classSpan;
    public ChooseTimeBean chooseTimeBean;
    public String time="10";
    public int span = 300;

    public String groupName ="";

    public TimeChooseAdapter.TimeListener timeListener ;
    public CreateSheetGroupAdapter(Context context) {
        super(context);
    }


    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return true;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return R.layout.footer_create_gruop;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.header_create_attance;
    }

    @Override
    public BaseViewHolder<PoiItem> getViewHolder(int viewType, View view) {
        if (viewType == TYPE_CHILD) {


            return  new AdddressViewHolder(view);
        }
        return null;
    }
    @Override
    public void onBindFooterViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition) {
        super.onBindFooterViewHolder(holder, groupPosition);
        holder.itemView.findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(timeListener==null){

                }else {
                    timeListener.doneClick();
                }
            }
        });
    }
    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_create_attance_cell;
    }

    @Override
    public void onBindChildViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition, int childPosition) {
        super.onBindChildViewHolder(holder, groupPosition, childPosition);
        AdddressViewHolder holder1 = (AdddressViewHolder) holder;
        holder1.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getData().remove(childPosition);
                notifyDataChanged();
            }
        });
    }

    @Override
    public void onBindHeaderViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition) {

        holder.itemView.findViewById(R.id.l_join).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Gson gson = new Gson();

                Bundle bundle = new Bundle();
                bundle.putString("param", gson.toJson(members));
                ActivityUtil.startActivity((Activity) mContext, AttanceMemberActivity.class, bundle, 1000);
            }
        });
        holder.itemView.findViewById(R.id.l_class).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ActivityUtil.startActivity((Activity) mContext, ClassActivity.class, new Bundle(), 1001);
            }
        });
        EditText tvJiaban = holder.itemView.findViewById(R.id.tv_jiabanshijian);
        tvJiaban.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                time =tvJiaban.getText().toString();

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        holder.itemView.findViewById(R.id.l_jiabanshijian).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ArrayList<String> item1 = new ArrayList<>();

                for (int i=1;i<7;i++){
                    item1.add(""+i*10);
                }

                OptionsPickerView optionsPickerView = Dialog.popWheel(mContext, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {

                        time =item1.get(options1);
                        tvJiaban.setText(time);

                    }
                });
                int index = 0;
                if (item1.contains(time)) {
                    index = item1.indexOf(time);
                } else {

                }


                optionsPickerView.setSelectOptions(index);
                optionsPickerView.setPicker(item1);
                optionsPickerView.show();
            }
        });

        holder.itemView.findViewById(R.id.l_riqi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                Bundle param = new Bundle();
                if (chooseTimeBean == null) {
                    if(classSpan!=null){
                        param.putString("span", gson.toJson(classSpan));
                    }
                } else {

                    param.putString("param", gson.toJson(chooseTimeBean));

                    if(classSpan!=null){
                        param.putString("span", gson.toJson(classSpan));
                    }
                }
                ActivityUtil.startActivity((Activity) mContext, TimeChooseActivity.class, param, 1002);
            }
        });
        holder.itemView.findViewById(R.id.l_address).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtil.startActivity((Activity) mContext, AddressChooseActivity.class, new Bundle(), 1003);

            }
        });
        TextView tvRange = holder.itemView.findViewById(R.id.tv_range);
        holder.itemView.findViewById(R.id.l_range).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ArrayList<String> arrayList = new ArrayList<>();
                for (int i = 1; i < 11; i++) {
                    arrayList.add((i * 100) + "米");
                }
                OptionsPickerView optionsPickerView = Dialog.popWheel(mContext, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {


                        String res = arrayList.get(options1);

                        span = Integer.valueOf(res.substring(0, res.length() - 1));
                        tvRange.setText(res.substring(0, res.length() - 1) + "米");


                    }
                });
                int index = 0;
                if (arrayList.contains(span + "米")) {
                    index = arrayList.indexOf(span + "米");
                } else {

                }


                optionsPickerView.setSelectOptions(index);
                optionsPickerView.setPicker(arrayList);
                optionsPickerView.show();

            }
        });

        TextView member = holder.itemView.findViewById(R.id.tv_member);
        TextView tvClass = holder.itemView.findViewById(R.id.tv_class);
        TextView tvTimes = holder.itemView.findViewById(R.id.tv_times);

        EditText et_group_name = holder.itemView.findViewById(R.id.et_group_name);
        et_group_name.setText(groupName);
        et_group_name.setSelection(groupName.length());
        tvJiaban.setText(time);
        et_group_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                groupName =et_group_name.getText().toString();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        tvRange.setText(span + "米");
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < members.size(); i++) {
            arrayList.add(members.get(i).getUsernickname());

        }
        member.setText(StringUtils.join(arrayList, ","));

        if (classSpan != null) {
            tvClass.setText(classSpan.getAttendacetimespanname() + " " + classSpan.getAttendacetimespan_begintime() + "-" + classSpan.getAttendacetimespan_endtime() + " " + classSpan.getHours());
        } else {
            tvClass.setText("请选择班次");
        }
        if (chooseTimeBean != null) {
            tvTimes.setText(chooseTimeBean.getTimeString());
        } else {
            tvTimes.setText("请选择考勤日期");
        }


    }


    public class AdddressViewHolder extends BaseViewHolder<PoiItem> {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_address)
        TextView tvAddress;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        public AdddressViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        @Override
        public void setData(PoiItem data) {
            tvName.setText(data.getTitle());
            tvAddress.setText(data.getSnippet());

        }
    }


}
